package com.itheima02;

import com.domain.Student;

import java.util.ArrayList;

/**
 * @author Xiao
 * @date 2021/7/6
 */
public class ChoiceStu_ArrayList {
    /**
        集合数据筛选
            定义一个方法，方法接收一个集合对象（泛型为Student），
            方法内部将年龄低于18的学生对象找出并存入新集合对象，方法返回新集合。
     */
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("阿良",24);
        Student stu2 = new Student("平安",21);
        Student stu3 = new Student("左右",25);
        Student stu4 = new Student("大水怪",15);
        Student stu5 = new Student("小天使",16);
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        System.out.println("筛选前集合元素：");
        printList(list);
        System.out.println("-------------");
        ArrayList<Student> list1 = choiceStu(list);
        System.out.println("筛选后集合元素：");
        printList(list1);
    }

    private static ArrayList<Student> choiceStu(ArrayList<Student> list) {
        ArrayList<Student> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getAge() < 18) {
                list1.add(stu);
            }
        }
        return list1;
    }

    private static void printList(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getName() + "\t" + stu.getAge());
        }
    }
}
